package com.gameplatform.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gameplatform.model.Comment;
import com.gameplatform.service.CommentService;
import com.gameplatform.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class CommentServlet extends HttpServlet {
    private CommentService commentService = new CommentService();
    private ObjectMapper objectMapper = new ObjectMapper();
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String gameIdStr = request.getParameter("gameId");
        
        if (gameIdStr != null) {
            try {
                int gameId = Integer.parseInt(gameIdStr);
                List<Comment> comments = commentService.getCommentsByGameId(gameId);
                ResponseUtil.writeResponse(response, 200, "success", comments);
            } catch (NumberFormatException e) {
                ResponseUtil.writeResponse(response, 400, "Invalid game ID", null);
            }
        } else {
            List<Comment> comments = commentService.getAllComments();
            ResponseUtil.writeResponse(response, 200, "success", comments);
        }
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        Comment comment = objectMapper.readValue(request.getInputStream(), Comment.class);
        boolean success = commentService.createComment(comment);
        
        if (success) {
            ResponseUtil.writeResponse(response, 201, "Comment created successfully", comment);
        } else {
            ResponseUtil.writeResponse(response, 500, "Failed to create comment", null);
        }
    }
    
    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        
        if (pathInfo != null && pathInfo.startsWith("/")) {
            String[] parts = pathInfo.split("/");
            if (parts.length == 2) {
                try {
                    int commentId = Integer.parseInt(parts[1]);
                    boolean success = commentService.deleteComment(commentId);
                    
                    if (success) {
                        ResponseUtil.writeResponse(response, 200, "Comment deleted successfully", null);
                    } else {
                        ResponseUtil.writeResponse(response, 500, "Failed to delete comment", null);
                    }
                } catch (NumberFormatException e) {
                    ResponseUtil.writeResponse(response, 400, "Invalid comment ID", null);
                }
            }
        }
    }
}